package com.qingneng.budget.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.budget.service.sdk.IRoleService;
import com.qingneng.budget.util.BaseAction;
import com.qingneng.budget.util.DigestUtils;
import com.qingneng.budget.vo.Role;
import com.qingneng.budget.vo.Userinfo;

public class SaveRoleAction extends BaseAction implements ModelDriven<Role>{
	
	private static final Logger logger = Logger.getLogger(SaveRoleAction.class);
	
	private Role role = new Role();
	

	

	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}


	private String message = null;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	// init service
	private IRoleService roleService;
	
	// 构造函数
	public SaveRoleAction(IRoleService roleService) {
		this.roleService = roleService;
	}

	public SaveRoleAction() {

	}
	
	/**
	 * save role data 
	 * @return
	 */
	public String saveData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin save role +++");
		}
		logger.info("role_name:"+role.getRole_name());
		
		
		try {
			
			int role_id  = role.getRole_id();
			//修改
			if(role_id>0){
				roleService.modify(role);
			}else{
				roleService.add(role);
			}
			role.setMessage("success");
		} catch (Exception e) {
			role.setMessage("添加角色失败");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end save role +++");
		}
		return Action.SUCCESS;
	}
	
	public String destoryData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin delete role +++");
		}
		try {
			roleService.destory(role);
			role.setMessage("success");
		} catch (Exception e) {
			role.setMessage("删除角色失败");
			e.printStackTrace();
		}
		
		if(logger.isDebugEnabled()){
			logger.debug("+++end delete role +++");
		}
		return Action.SUCCESS;
	}

	public Role getModel() {
		// TODO Auto-generated method stub
		return role;
	}

}
